﻿package com.projects.xenia.ref_classes;

import java.util.ArrayList;
import java.util.HashMap;

import android.database.Cursor;

import com.projects.xenia.global_modules.MainGlobalModule;

public class GoodRefObject {
	
	public ArrayList <DiscountRefItem> DiscountRef;
	public HashMap<String, DiscountRefItem> DiscountRefIndx;
	
	public GoodRefObject (String CustomerId) {
				
		DiscountRef = new ArrayList <DiscountRefItem> ();
		DiscountRefIndx = new HashMap<String, DiscountRefItem>();
		if (CustomerId!=null) {
			GetCustomersDiscount (CustomerId);
		}
		
	}
	 	
	
	public double GetGoodsPrice (String GoodsId,String PriceCategory) {
		
		double _price = 0.0;		
			
		if (PriceCategory == null){
			PriceCategory = MainGlobalModule.ConstantsCollection.get("DefPriceCategory");
		}	
	
		Cursor GoodsCursor = MainGlobalModule.DBE.rawQuery("select price FROM xref_goodsprices where goods_id = ? and pricecategory_id = ?", new String [] {GoodsId,PriceCategory});
		
		if (GoodsCursor.getCount()>0){
			GoodsCursor.moveToFirst();
			_price = GoodsCursor.getDouble(0);
		}	
		
		return (_price);
		
	}
	
	
	public double GetPriceDiscount (String GoodsId,DiscountRefItem dri) {
		
		double _price = 0.0;		
		_price = GetGoodsPrice (GoodsId,dri.PriceCategory);		
		if (dri.Discount!=0) {
			_price -= _price*dri.Discount/100;			
		}		
		return (_price);		
	}
	
	
	private void GetCustomersDiscount (String CustomerId) {
		
		Cursor DiscountCursor = MainGlobalModule.DBE.rawQuery("select goods_id , discount , pricecategory_id, is_default FROM xref_CustomersDiscounts where customers_id = ? order by is_default desc", new String [] {CustomerId});		
		if(DiscountCursor != null && DiscountCursor.getCount() > 0){
			DiscountCursor.moveToFirst();
			do{
				DiscountRefItem dri = new DiscountRefItem();
				dri.GoodsId = DiscountCursor.getString(0);
				dri.Discount = DiscountCursor.getDouble(1);
				dri.PriceCategory = DiscountCursor.getString(2);				
				dri.Default = DiscountCursor.getInt(3) == 1;				
				DiscountRef.add(dri);
				if (dri.GoodsId.equals("")==false) {
					DiscountRefIndx.put(dri.GoodsId, dri);	
				}
			}
			while(DiscountCursor.moveToNext());
		}		
		
	}
	
	private String GetParent (String GoodsId) {		
		Cursor GoodsCursor = MainGlobalModule.DBE.rawQuery("select parentid FROM xref_goods where id = ?", new String [] {GoodsId});
		if(GoodsCursor != null && GoodsCursor.getCount() > 0){
			GoodsCursor.moveToFirst();
			String _id = GoodsCursor.getString(0);
			GoodsCursor.close();
			return (_id);
		}
		else {
			return (null);
		}	
	}
	
	public double GetGoodPriceByDiscount (String GoodsId) {
		
		double _price = 0.0;
		
		if (DiscountRefIndx.size() > 0) {
			String _parentid = GoodsId;
			DiscountRefItem ref;
			while (_parentid!=null) {
				_parentid = GetParent (_parentid);				
				ref = DiscountRefIndx.get (_parentid);
				if (ref != null) {
					double _price0 = GetPriceDiscount (GoodsId,ref);
					if (_price0!=0) {
						return (_price0);
					}
				}				
			}
		}		
		
		for (int i = 0;i<DiscountRef.size();i++) {		
			DiscountRefItem dri = DiscountRef.get(i);
			if (dri.GoodsId.equals("")) {
				double _price0 = GetPriceDiscount (GoodsId,dri);
				if (_price0!=0) {
					_price = _price0;
				}
			}
		}
		
		return (_price);
		
	}	
		
}
